/*
 * Copyright (C) 2009 Teleca Poland Sp. z o.o. <android@teleca.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.uneed.adapter;

import com.uneed.R;
import com.uneed.api.Shop;
import com.uneed.widget.RemoteImageView;

import android.R.integer;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * Adapter representing albums
 * 
 * @author Lukasz Wisniewski
 */
public class ShopAdapter extends ArrayListAdapter<Shop> {
	
	public ShopAdapter(Activity context) {
		super(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row=convertView;

		ViewHolder holder;

		if (row==null) {
			LayoutInflater inflater = mContext.getLayoutInflater();
			row=inflater.inflate(R.layout.shop_list_row, null);

			holder = new ViewHolder();
			holder.image = (RemoteImageView)row.findViewById(R.id.shopImageView);
			holder.shopName = (TextView)row.findViewById(R.id.titleTextView);
			holder.shopSubName = (TextView)row.findViewById(R.id.subTitleTextView);
//			holder.progressBar = (ProgressBar)row.findViewById(R.id.AlbumRowRatingBar);
			holder.distance = (TextView)row.findViewById(R.id.distTextView);
			holder.description = (TextView)row.findViewById(R.id.shopdesc);
		
			row.setTag(holder);
		}
		else{
			holder = (ViewHolder) row.getTag();
		}
		
		holder.image.setDefaultImage(R.drawable.placeholder_loading);
		holder.image.setEmptyImage(R.drawable.placeholder_empty);
		holder.image.setImageUrl(mList.get(position).getThumb(),position, getListView());
		holder.shopName.setText(mList.get(position).getName());
		holder.shopSubName.setText(String.valueOf(mList.get(position).getSubname()));
		String dist_str = "";
		float dist = mList.get(position).getDistance();
		if (dist<100)
			dist_str = "<100m";
		else if (dist<1000) 
			dist_str = ((int)dist) + "m";
		else
			dist_str = ((int)dist/100)/10.0 + "km";
		holder.distance.setText(dist_str);
		holder.description.setText(String.valueOf(mList.get(position).getCategoryname()));

		//		holder.progressBar.setMax(10);
//		holder.progressBar.setProgress((int) (mList.get(position).getRating()*10));

		return row;
	}
	
	/**
	 * Class implementing holder pattern,
	 * performance boost
	 * 
	 * @author Lukasz Wisniewski
	 */
	static class ViewHolder {
		RemoteImageView image;
		TextView shopName;
		TextView shopSubName;
		ProgressBar progressBar;
		TextView distance;
		TextView description;
	}
}
